<?php

namespace App\Http\Controllers\Backend\Goods;

use App\Models\Goods\GoodsCate;
use App\Models\Goods\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsCateController extends Controller
{
    /**
     * goodsCateList 商品分类列表
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function goodsCateList(Request $request){
        $type = $request->type ? 1 : 0;
        $category = new GoodsCate;
        $lists = $type ? $category->getCategoryInfo(false):$category->getCategoryInfo();
        return view('backend.goods.goodscatelist',[
            'lists'=>$lists,
            'type'=>$type,
        ]);
    }

    /**
     * addGoodsCate 增加商品分类
     *
     * @param Request $request
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function addGoodsCate(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            $validator = GoodsCate::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/goodsCateAdd')
                    ->withErrors($validator)
                    ->withInput();
            }
            GoodsCate::create($data);
            return redirect('/admin/goodsCateList');
        }else{
            $category = new GoodsCate;
            $items = $category->getCategoryInfo();
            return view('backend.goods.goodscateadd',['action'=>'add','gclist'=>$items]);
        }
    }

    /**
     * updateGoodsCate 更新商品分类
     *
     * @param Request $request
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function updateGoodsCate(Request $request){
        if($request->isMethod('post')){
            $validator = GoodsCate::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/goodsCateUpdate/id/'.$request->id)
                    ->withErrors($validator)
                    ->withInput();
            }
            $save = GoodsCate::find($request->id);
            $pid = $request->pid == $save->id ?$save->pid:$request->pid;
            $save->pid = $pid;
            $save->name = $request->name;
            $save->description = $request->description;
            $save->save();
            return redirect('/admin/goodsCateList');
        }else{
            $category = new GoodsCate;
            $items = $category->getCategoryInfo();
            $item =  GoodsCate::find($request->id);
            return view('backend.goods.goodscateadd',['action'=>'update','item'=>$item,'gclist'=>$items]);
        }
    }

    /**
     * delGoodsCate 删除商品分类-软删除
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function delGoodsCate(Request $request){
        $childs = GoodsCate::find($request->id)->childCategory->isEmpty();
        $goodsCount = Goods::where('cate_id','=',$request->id)->count();
        if(!$childs){
            abort(500,'该分类下面含有子级分类，请先删除子级分类！');
        }
        if($goodsCount>0){
            abort(500,'该分类下面含有商品，请先删除该分类下的商品！');
        }
        GoodsCate::where('id', $request->id)->delete();
        return redirect('/admin/goodsCateList');
    }

    /**
     * restoreGoodsCate 还原商品分类
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restoreGoodsCate(Request $request){
        $getData = GoodsCate::onlyTrashed()->find($request->id);
        $count = GoodsCate::onlyTrashed()->where('id','=',$getData->pid)->count();
        if($count>0){
            abort(500,'请先恢复父级菜单，以便列表可以正常显示！');
        }
        GoodsCate::withTrashed()->where('id', $request->id)->restore();
        return back();
    }

    /**
     * forceDelGoodsCate 从回收站删除父级栏目-物理删除
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function forceDelGoodsCate(Request $request){
        $count = GoodsCate::withTrashed()->where('pid','=',$request->id)->count();
        $goodsCount = Goods::where('cate_id','=',$request->id)->count();
        if($count>0){
            abort(500,'该分类下面含有子级分类，请先删除子级分类！');
        }
        if($goodsCount>0){
            abort(500,'该分类下面含有商品，请先删除该分类下的商品！');
        }
        GoodsCate::withTrashed()->where('id', $request->id)->forceDelete();
        return back();
    }
}
